from pymongo import MongoClient

conn_mongo = MongoClient('10.188.188.22', 27017)
db = conn_mongo.eLMSG

# for item in db.eLMSG.find({bacdive_backup":{"$exists":1}}):
for item in db.eLMSG.find({}):
    if 'bacdive_backup' in item.keys():
        # print(item)
        new_item = item
        # print(item['bacdive_backup']['morphology_physiology'].keys())
        if 'cell_morphology' in item['bacdive_backup']['morphology_physiology'].keys():
            cell_morphologys = item['bacdive_backup']['morphology_physiology']['cell_morphology']
            # print(cell_morphologys)
            for shape_size in cell_morphologys:
                # print(shape_ size['cell_shape'])
                size = {}
                shape = {}
                if shape_size['cell_shape'] is not None:
                    shape['ID_reference'] = shape_size['ID_reference']
                    shape['cell_shape'] = shape_size['cell_shape']
                    new_item.setdefault('Morphology', {}).setdefault('cell_shape', []).append(shape)
                if shape_size['cell_len'] is not None or shape_size['cell_width'] is not None:
                    size['ID_reference'] = shape_size['ID_reference']
                    size['cell_len'] = shape_size['cell_len']
                    size['cell_len_unit'] = shape_size['cell_len_unit']
                    size['cell_width'] = shape_size['cell_width']
                    size['cell_width_unit'] = shape_size['cell_width_unit']
                    new_item.setdefault('Morphology', {}).setdefault('cell_size', []).append(size)
                if shape_size['motility'] is not None:
                    mo = {}
                    mo['ID_reference'] = shape_size['ID_reference']
                    mo['motility'] = shape_size['motility']
                    new_item.setdefault('Morphology', {}).setdefault('motility', []).append(mo)
                if shape_size['flagellum_arrangement'] is not None:
                    arr = {}
                    arr['ID_reference'] = shape_size['ID_reference']
                    arr['flagellum_arr'] = shape_size['flagellum_arrangement']
                    new_item.setdefault('Morphology', {}).setdefault('flagellum_arr', []).append(arr)
                    # new_item.setdefault('Morphology', {}).setdefault('motility', []).append(shape_size['motility'])
                    # new_item.setdefault('Morphology', {}).setdefault('flagellum_arr', []).append(shape_size['flagellum_arrangement'])
        if 'spore_formation' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Morphology', {})['spore_form'] = item['bacdive_backup']['morphology_physiology'][
                'spore_formation']

        if 'multicellular_morphology' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Morphology', {})['multicellular_morphology'] = \
            item['bacdive_backup']['morphology_physiology']['multicellular_morphology']

        if 'Pigmentation' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Morphology', {})['pigmentation'] = item['bacdive_backup']['morphology_physiology'][
                'Pigmentation']

        if 'colony_morphology' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Morphology', {})['colony_morphology'] = \
            item['bacdive_backup']['morphology_physiology']['colony_morphology']

        if 'murein' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Morphology', {})['murein'] = item['bacdive_backup']['morphology_physiology']['murein']

        if 'culture_medium' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['culture_medium'] = item['bacdive_backup']['morphology_physiology'][
                'culture_medium']

        if 'Observation' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['observation'] = item['bacdive_backup']['morphology_physiology'][
                'Observation']

        if 'met_antibiotica' in item['bacdive_backup']['morphology_physiology'].keys():
            met_antibioticas = item['bacdive_backup']['morphology_physiology']['met_antibiotica']
            for met_antibiotica in met_antibioticas:
                antibiotica = {}
                antibiotica['ID_reference'] = met_antibiotica['ID_reference']
                antibiotica['metabolite_antib'] = met_antibiotica['metabolite_antib']
                antibiotica['ab_intermediate'] = met_antibiotica['ab_intermediate']
                antibiotica['ab_intermediate_conc'] = met_antibiotica['ab_intermediate_conc']
                antibiotica['ab_intermediate_unit'] = met_antibiotica['ab_intermediate_unit']
                new_item.setdefault('Physiology', {}).setdefault('met_antibiotica', []).append(antibiotica)

        if 'met_antibiogram' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['met_antibiogram'] = item['bacdive_backup']['morphology_physiology'][
                'met_antibiogram']

        if 'nutrition_type' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['nutrition_type'] = item['bacdive_backup']['morphology_physiology'][
                'nutrition_type']

        if 'met_production' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['met_production'] = item['bacdive_backup']['morphology_physiology'][
                'met_production']

        if 'tolerance' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['tolerance'] = item['bacdive_backup']['morphology_physiology'][
                'tolerance']

        if 'oxygen_tolerance' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['oxygen'] = item['bacdive_backup']['morphology_physiology'][
                'oxygen_tolerance']

        if 'cell_morphology' in item['bacdive_backup']['morphology_physiology'].keys():
            grams = item['bacdive_backup']['morphology_physiology']['cell_morphology']
            for gram in grams:
                gram_tmp = {}
                if gram['gram_stain'] is not None:
                    gram_tmp['ID_reference'] = gram['ID_reference']
                    gram_tmp['gram'] = gram['gram_stain']
                    new_item.setdefault('Physiology', {}).setdefault('gram', []).append(gram_tmp)

        if 'culture_temp' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['growth_temp'] = item['bacdive_backup']['morphology_physiology'][
                'culture_temp']

        if 'culture_pH' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['growth_ph'] = item['bacdive_backup']['morphology_physiology'][
                'culture_pH']

        if 'halophily' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['halophily'] = item['bacdive_backup']['morphology_physiology'][
                'halophily']

        if 'met_util' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['cn_source'] = item['bacdive_backup']['morphology_physiology'][
                'met_util']

        if 'GC_content' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['gc'] = item['bacdive_backup']['morphology_physiology']['GC_content']

        if 'met_antibiotica' in item['bacdive_backup']['morphology_physiology'].keys():
            met_antibioticas = item['bacdive_backup']['morphology_physiology']['met_antibiotica']
            for met_antibiotica in met_antibioticas:
                sensitive = {}
                sensitive['ID_reference'] = met_antibiotica['ID_reference']
                sensitive['metabolite_antib'] = met_antibiotica['metabolite_antib']
                sensitive['ab_sensitive'] = met_antibiotica['ab_sensitive']
                sensitive['ab_sensitivity_conc'] = met_antibiotica['ab_sensitivity_conc']
                sensitive['ab_sensitivity_unit'] = met_antibiotica['ab_sensitivity_unit']
                new_item.setdefault('Physiology', {}).setdefault('antibiotic_s', []).append(sensitive)

                resistant = {}
                resistant['ID_reference'] = met_antibiotica['ID_reference']
                resistant['metabolite_antib'] = met_antibiotica['metabolite_antib']
                resistant['ab_resistant'] = met_antibiotica['ab_resistant']
                resistant['ab_resistance_conc'] = met_antibiotica['ab_resistance_conc']
                resistant['ab_resistance_unit'] = met_antibiotica['ab_resistance_unit']
                new_item.setdefault('Physiology', {}).setdefault('antibiotic_r', []).append(resistant)

        if 'risk_assessment' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['pathogenicity'] = item['bacdive_backup']['morphology_physiology'][
                'risk_assessment']

        if 'sequence' in item['bacdive_backup']['morphology_physiology'].keys():
            new_item.setdefault('Physiology', {})['sequence'] = item['bacdive_backup']['morphology_physiology'][
                'sequence']

        if 'met_test' in item['bacdive_backup']['morphology_physiology'].keys():
            met_tests = item['bacdive_backup']['morphology_physiology']['met_test']
            for met_test in met_tests:
                test_tmp = {}
                if met_test['indole_test'] is not None:
                    test_tmp['ID_reference'] = met_test['ID_reference']
                    test_tmp['indole_pdu'] = met_test['indole_test']
                    new_item.setdefault('Biochemistry', {}).setdefault('indole_pdu', []).append(test_tmp)
                    # new_item.setdefault('Physiology', {})['indole_pdu']= item['bacdive_backup']['morphology_physiology']['met_test']
                vp = {}
                if met_test['voges_proskauer'] is not None:
                    vp['ID_reference'] = met_test['ID_reference']
                    vp['vp_test'] = met_test['voges_proskauer']
                    new_item.setdefault('Biochemistry', {}).setdefault('vp_test', []).append(vp)
                citrate = {}
                if met_test['citrate_test'] is not None:
                    citrate['ID_reference'] = met_test['ID_reference']
                    citrate['citrate_utl'] = met_test['citrate_test']
                    new_item.setdefault('Biochemistry', {}).setdefault('citrate_utl', []).append(citrate)
                metylred = {}
                if met_test['methyl_red'] is not None:
                    metylred['ID_reference'] = met_test['ID_reference']
                    metylred['metylred_test'] = met_test['methyl_red']
                    new_item.setdefault('Biochemistry', {}).setdefault('metylred_test', []).append(metylred)
        if 'enzymes' in item['bacdive_backup']['morphology_physiology'].keys():
            enzymes = item['bacdive_backup']['morphology_physiology']['enzymes']
            # print(enzymes)
            for enzyme in enzymes:
                tmp_enzyme = {}
                enzyme_name = enzyme['enzyme']
                tmp_enzyme['ID_reference'] = enzyme['ID_reference']
                tmp_enzyme['activity'] = enzyme['activity']
                tmp_enzyme['ec_number'] = enzyme['ec_number']
                new_item.setdefault('Enzymology', {})[enzyme_name] = tmp_enzyme

        if 'origin' in item['bacdive_backup']['environment_sampling_isolation_source'].keys():
            new_item['environment_sampling_isolation_source'] = item['bacdive_backup'][
                'environment_sampling_isolation_source']

        if 'strain_availability' in item['bacdive_backup'].keys():
            new_item['strain_availability'] = item['bacdive_backup']['strain_availability']
        del new_item['bacdive_backup']
        db.eLMSG_1112.save(new_item)
    else:
        db.eLMSG_1112.save(item)
